from BruTile.Pointf import Pointf
from BruTile.Extent import Extent

class MapTransformHelpers(object):
    '''
    classdocs
    '''

    def __init__(self):
        '''Constructor'''
    
    @staticmethod
    def pan(map_transform, current_map_pos, previous_map_pos):
        current_pos = map_transform.mapToWorld(current_map_pos.x, current_map_pos.y)
        previous_pos = map_transform.mapToWorld(previous_map_pos.x, previous_map_pos.y)
        
        diff_x = previous_pos.x - current_pos.x
        diff_y = previous_pos.y - current_pos.y
        
        map_transform.setCenter(Pointf((map_transform.center.x + diff_x),
                                        (map_transform.center.y + diff_y)))
    
    @staticmethod
    def recalculateBBExtent(bb_extent, tile_schema):
        min_x = min(bb_extent.getMinX(), bb_extent.getMaxX())
        min_y = min(bb_extent.getMinY(), bb_extent.getMaxY())
        max_x = max(bb_extent.getMinX(), bb_extent.getMaxX())
        max_y = max(bb_extent.getMinY(), bb_extent.getMaxY())
        
        min_x = max(min_x, tile_schema.extent.getMinX())
        min_y = max(min_y, tile_schema.extent.getMinY())
        max_x = min(max_x, tile_schema.extent.getMaxX())
        max_y = min(max_y, tile_schema.extent.getMaxY())
        
        min_x = min(min_x, tile_schema.extent.getMaxX())
        min_y = min(min_y, tile_schema.extent.getMaxY())
        max_x = max(max_x, tile_schema.extent.getMinX())
        max_y = max(max_y, tile_schema.extent.getMinY())

        return Extent(min_x, min_y, max_x, max_y)
    
    @staticmethod
    def checkForSmallerCoverage(map_transform, extent):
        width = (extent.getMaxX() - extent.getMinX()) / 256
        print("width = " + str(width))